package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.consts.OrderStatus;
import com.shop.entity.Order;
import com.shop.mapper.OrderMapper;
import com.shop.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("orderService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;

    @Override
    public PageInfo<Order> listOrder(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(orderMapper.listOrder(pageNum, pageSize));
    }

    @Override
    public Order getOrderById(int id) {
        return orderMapper.getOrderById(id);
    }
    public Order getOrderByOrderId(String id) {
        return orderMapper.getOrderByOrderId(id);
    }

    @Override
    public void addOrder(Order order) {
        orderMapper.addOrder(order);
    }

    @Override
    public void updateOrder(Integer id, String status) {
        orderMapper.updateOrder(id, status);
    }

    @Override
    public void deleteOrder(Integer id) {
        orderMapper.deleteOrder(id);
    }

    @Override
    public int getTotal() {
        return orderMapper.getTotal();
    }

    @Override
    public int countOrderMoney() {
        return orderMapper.countOrderMoney();
    }

    @Override
    public PageInfo<Order> getTotalStatus(String status, Integer pageNum, Integer pageSize) {
        return new PageInfo<>(orderMapper.getTotalStatus(status, pageNum, pageSize));
    }

    @Override
    public List<Order> getOrderByUserId(Integer id) {
        return orderMapper.getOrderByUserId(id);
    }

    @Override
    public PageInfo<Order> getOrderByIdAndStatus(Integer id, String status, Integer pageNum, Integer pageSize) {
        return new PageInfo<>(orderMapper.getOrderByIdAndStatus(id, status, pageNum, pageSize));
    }

    @Override
    public boolean isValidOrder(String orderId) {
        Order order = orderMapper.getOrderByOrderId(orderId);
        if (order == null) {
            return false;
        }
        return OrderStatus.PENDING.getCode().equals(order.getStatus());
    }

    @Override
    public void updateOrderStatus(String outTradeNo, String tradeNo, String totalAmount) {
        if (outTradeNo == null || outTradeNo.isEmpty()) {
            throw new IllegalArgumentException("outTradeNo 不能为空");
        }
        if (tradeNo == null || tradeNo.isEmpty()) {
            throw new IllegalArgumentException("tradeNo 不能为空");
        }
        if (totalAmount == null || !totalAmount.matches("^\\d+(\\.\\d{1,2})?$")) {
            throw new IllegalArgumentException("totalAmount 必须是合法的金额格式（最多两位小数）");
        }

        // 更新订单状态为待发货
        orderMapper.updateOrderStatus(outTradeNo, OrderStatus.SHIPMENT.getCode(), tradeNo);
    }

    @Override
    public boolean isOrderPaid(String outTradeNo) {
        String status = orderMapper.getOrderStatusById(outTradeNo);
        return !OrderStatus.PENDING.getCode().equals(status) && !OrderStatus.CANCELLED.getCode().equals(status);
    }

}
